home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / basics / show movie / windstuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  7.4 KB  |  268 lines

  1. /*
  2.     File:        WindStuff.c
  3.  
  4.     Contains:    Window handling routines.
  5.  
  6.     Written by: Jason Hodges-Harris & Don Swatman    
  7.  
  8.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/17/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include <Dialogs.h>
  25.  
  26. #include "WindStuff.h"
  27.  
  28. #include "MenuStuff.h"
  29. #include "MovieStuff.h"
  30.  
  31. //==============================================
  32. //  Globals                                     
  33. //==============================================
  34. WindowPtr gTheWinds[kMaxWindows];  // holds the list of windows
  35. Boolean      gDone;                   // Set to true to make the program quit
  36.  
  37. //==============================================
  38. //  AboutBox                                   
  39. //
  40. // This draws the about box. It also shows how to
  41. // use the tool box to draw a default box around
  42. // the OK box
  43. //==============================================
  44.  
  45. void AboutBox(void)
  46. {
  47.   GrafPtr   savePort = nil;
  48.     DialogPtr aboutDialog;
  49.     ModalFilterUPP theFilter = nil;
  50.     short     itemHit = 0;   // dialog item we've clicked on
  51.     
  52. // Get the dialog box resource
  53.     aboutDialog = GetNewDialog(9041, nil, (WindowPtr) -1 );
  54.  
  55.   GetPort(&savePort);
  56.   SetPort( aboutDialog );
  57.  
  58.     ShowWindow( aboutDialog );
  59.         
  60. // Get the standard filter proc
  61.   if (GetStdFilterProc(&theFilter) != noErr)
  62.       DebugStr("\pFailed to get standard dialog filter.");
  63.   
  64. // Set item 1 - <OK> to have a default box around it
  65.     SetDialogDefaultItem(aboutDialog,1);
  66.   
  67. // Modal dialog loop    
  68. do     {
  69. // Use "theFilter" in ModalDialog call
  70.        ModalDialog(theFilter,&itemHit);
  71.     } while (itemHit != 1);
  72.  
  73.   DisposeDialog(aboutDialog);
  74.   SetPort(savePort);
  75. }
  76.  
  77. //==============================================
  78. //
  79. //  Gemeral Window handling Stuff
  80. //
  81. //==============================================
  82.  
  83. //----------------------------------------------
  84. //  GetWindowNum
  85. //
  86. // Gets the number of a window in the list from
  87. // it's WindowPtr
  88. //----------------------------------------------
  89. short GetWindowNum ( WindowPtr pWindow )
  90. {
  91.     short windCount;
  92.     short foundWind = -1;
  93.  
  94.     if (pWindow)
  95.         for (windCount=0; (windCount < kMaxWindows) && (foundWind == -1); windCount++)
  96.             if (gTheWinds[windCount] == pWindow)
  97.                 foundWind = windCount;
  98.     
  99.     return ( foundWind);
  100. }
  101.  
  102. //----------------------------------------------
  103. //  IsOurWindow
  104. //
  105. // Find out if this is one of our window
  106. //----------------------------------------------
  107. Boolean IsOurWindow ( WindowPtr pWindow )
  108. {
  109.     return ( GetWindowNum(pWindow) != -1 );
  110. }
  111.  
  112. //----------------------------------------------
  113. // IsFreeWind
  114. //
  115. // Find out if we can add "howManyNeeded" more windows
  116. //   and return a number to the first free
  117. //----------------------------------------------
  118.  
  119. Boolean IsFreeWind( short *newWindNum, short howManyNeeded )
  120. {
  121.     short windCount;   // Current window list entry we're looking at
  122.     short leftToFind;  // How many more windows we need to find
  123.     
  124.     leftToFind  = howManyNeeded;
  125.     *newWindNum = -1;
  126.     for (windCount=0; (windCount < kMaxWindows) && (leftToFind != 0 ); windCount++)
  127.         if (gTheWinds[windCount] == nil)
  128.             {
  129.                 --leftToFind;
  130.                 if (*newWindNum == -1 )
  131.                     *newWindNum = windCount;
  132.             }
  133.     return(leftToFind == 0);
  134. }
  135.  
  136. //----------------------------------------------
  137. //  CloseOurWindow
  138. //
  139. // Close a window pointed to by "pWindow"
  140. // Also if the window is a master movie, then 
  141. // recursively close it's slave window
  142. //----------------------------------------------
  143. void CloseOurWindow ( short windNum )
  144. {
  145.     short     slaveWindNum = -1;   // slave window num
  146.     WindowPtr pSlaveWindow = nil;  // slave WindowPtr
  147.     
  148.     if (gTheWinds[windNum])                   // Check it's not nil
  149.         if (IsOurWindow(gTheWinds[windNum]))    // Check it's ours
  150.         {
  151.             CloseMovieWindow( gTheWinds[windNum],
  152.                                                 &pSlaveWindow );    // kills all the movie stuff
  153.             DisposeWindow(gTheWinds[windNum]);    // dispose of the window
  154.             gTheWinds[windNum] = nil;             // Return nil in pWindow            
  155.             DoAdjustMenus();                        // update the menus
  156.  
  157. // Recursively remove the window's slave
  158.             if (pSlaveWindow)
  159.             {
  160.                 slaveWindNum = GetWindowNum ( pSlaveWindow );
  161.                 if (slaveWindNum != -1 )
  162.                     CloseOurWindow ( slaveWindNum );
  163.             }
  164.         }
  165. }
  166.  
  167. //----------------------------------------------
  168. //   CloseAllWindows
  169. //
  170. //  Scans down the window list and closes each
  171. //  of the windows
  172. //----------------------------------------------
  173.  
  174. void CloseAllWindows(void)
  175. {
  176.     short windCount;
  177.  
  178.     for (windCount = 0; windCount < kMaxWindows; windCount++)
  179.         if (gTheWinds[windCount] != nil)
  180.             CloseOurWindow( windCount );
  181. }
  182.  
  183. //----------------------------------------------
  184. // DoWindUpdate
  185. //
  186. // Updates a window
  187. //----------------------------------------------
  188.  
  189. void    DoWindUpdate ( WindowPtr pWindow )
  190. {    
  191.     if (IsOurWindow(pWindow))
  192.     {
  193.         BeginUpdate (pWindow);
  194.  
  195. // update the movie window
  196.         UpdateMovieWindow ( pWindow );
  197.         
  198.         EndUpdate (pWindow);
  199.     }
  200. }
  201.  
  202. //----------------------------------------------
  203. //  DragSelWind
  204. //
  205. // Handles a click in the drag area of a window
  206. // and drags selected window around desktop
  207. //----------------------------------------------
  208. void DragSelWind( WindowPtr window,
  209.                                     Point     mouseLoc)
  210. {
  211.     Rect    dragBounds;
  212.     
  213.     dragBounds = (**GetGrayRgn()).rgnBBox;   // Get the rect the window can be dragged around
  214.     DragWindow(window,mouseLoc,&dragBounds); // Now Drag it
  215. }
  216.  
  217. //----------------------------------------------
  218. //  DoGoAwayWind
  219. //
  220. // Handles mouse down event in the go away box
  221. // and closes the window if neccessary
  222. //----------------------------------------------
  223. void DoGoAwayWind ( WindowPtr pWindow,
  224.                                         Point     mouseLoc)
  225. {
  226.     short windNum;
  227.     if (TrackGoAway(pWindow,mouseLoc))
  228.     {
  229.         if (pWindow)
  230.         {
  231.             windNum = GetWindowNum ( pWindow );
  232.             if (windNum != -1 )
  233.                 CloseOurWindow( windNum );
  234.         }
  235.     }
  236. }
  237.  
  238. //----------------------------------------------
  239. //   CreateWindow
  240. //
  241. // Create an empty window,
  242. //   but doesn't show it just yet
  243. //----------------------------------------------
  244.  
  245. void CreateWindow ( short windNum, Str255 theTitle, WindowPtr pWindowBehind )
  246. {
  247.     Rect  windRect;      // Initial window size
  248.     short diagOffset;    // offset from top left of window
  249.     
  250. // Calculate where we want to put the window and give it an initial size of 50,50
  251.     diagOffset = 50 * windNum;
  252.     SetRect( &windRect, 50,50,100,100 );
  253.     OffsetRect ( &windRect, diagOffset, diagOffset );
  254.  
  255.     gTheWinds[windNum] = (WindowPtr) NewCWindow ( nil,            // Create Storage
  256.                                                                                                  &windRect,      // Rect to put the window in
  257.                                                                                                  theTitle,       // Windows Title
  258.                                                                                                  false,          // Not Visible
  259.                                                                                                  noGrowDocProc,  // Ordinary window without grow box
  260.                                                                                                 pWindowBehind,  // Window this one behind
  261.                                                                                                  true,           // Has Go away box
  262.                                                                                                  0 );            // no Refcon (we'll use this later)
  263.                                                                                                                  //    to store movie info
  264.  
  265. }
  266.  
  267.  
  268.